#include "InsConfig.h"
#include <osgEarth/Config>
#include <fstream>
#include <QString>
#include <QStringList>


InsConfig::InsConfig()
{
	scene_type = "OSGEARTH";
	scene_path = "";

	camera_fov = -1;
	camera_viewheight = -1;
	camera_viewwidth = -1;

}

InsConfig* InsConfig::instance()
{
	static InsConfig gis;
	return &gis;
}

int InsConfig::readFile(std::string path)
{
	osgEarth::Config doc;
	//std::ifstream in("../user_data/hbt/config/sys.xml");
	std::ifstream in(path);
	if (!in.good()) return -1;

	if (!doc.fromXML(in)) {
		return -2;
	}
	const osgEarth::Config* cfg = doc.child_ptr("config");
	if (!cfg) return -3;

	{
		auto* c_scene = cfg->child_ptr("scene");
		if (c_scene) {
			if (c_scene->child_ptr("type")) {
				this->scene_type = c_scene->child_ptr("type")->value();
			}
			if (c_scene->child_ptr("path")) {
				this->scene_path = c_scene->child_ptr("path")->value();
			}
			auto* c_namefilter = c_scene->child_ptr("namefilter");
			if (c_namefilter) {
				this->scene_nameFilter = c_namefilter->value();
			}
			//float fv = osgEarth::as(cit->value(), 1.0);
			//this->stereo_scale = fv;
			auto* c_options = c_scene->child_ptr("options");
			if (c_options) {
				std::string opt_str = c_options->value();
				QString opt_str_q = QString::fromStdString(opt_str);

				QStringList opt_list = opt_str_q.split(";");
				for each (QString var in opt_list)
				{
					QStringList kv = var.split(":");
					if (kv.size() >= 2)
					{
						this->scene_options[kv[0].trimmed().toStdString()] = kv[1].trimmed().toStdString();
					}
					else if (kv.size() == 1)
					{
						this->scene_options[kv[0].trimmed().toStdString()] = kv[0].trimmed().toStdString();
					}
				}
			}
		}
	}

	{
		auto* cfg_2 = cfg->child_ptr("camera");
		if (cfg_2) {
			if (cfg_2->child_ptr("fov")) {
				this->camera_fov = osgEarth::as(cfg_2->child_ptr("fov")->value(),60.0);
			}
			if (cfg_2->child_ptr("view_width")) {
				this->camera_viewwidth = osgEarth::as(cfg_2->child_ptr("view_width")->value(), 512); 
			}
			if (cfg_2->child_ptr("view_height")) {
				this->camera_viewheight = osgEarth::as(cfg_2->child_ptr("view_height")->value(), 512);
			}
			//float fv = osgEarth::as(cit->value(), 1.0);
			//this->stereo_scale = fv;
		}
	}

	{
		auto* cfg_2 = cfg->child_ptr("capture");
		if (cfg_2) {
			if (cfg_2->child_ptr("type")) {
				this->capture_type = cfg_2->child_ptr("type")->value();
			}
			if (cfg_2->child_ptr("pathfile")) {
				this->capture_pathfile = cfg_2->child_ptr("pathfile")->value();
			}
			if (cfg_2->child_ptr("outdir")) {
				this->capture_outdir = cfg_2->child_ptr("outdir")->value();
			}
			if (cfg_2->child_ptr("outext")) {
				this->capture_outext = cfg_2->child_ptr("outext")->value();
			}
			if (cfg_2->child_ptr("interval")) {
				this->capture_interval = osgEarth::as<float>(cfg_2->child_ptr("interval")->value(), 1.0f);
			}
		}
	}

	in.close();
	return 0;
}
